<?php

namespace App\Models;

class Reply extends BaseModel
{
    protected $table = "reply";


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    public function getReply()
    {
        $res = $this->getList()->toArray();
        $data = '';
        if ($res) {
            $data = $res[0];
        }
        return $data;
    }

    public function modifyReply($request)
    {
        $id = $request['id'];
        $data['title'] = trim($request['title']);
        $data['description'] = trim($request['description']);
        $data['content'] = $request['content'] ? addslashes($request['content']) : '' ;
        $data['url'] = urlWeixin('wechat','getShowArticle');
        if ($request['pics']) {
            $imgPath = $this->upImgs($request['pics']);
            $data['pic'] = $imgPath;
        }
        if ($id) {
            $where['id'] = $id;
            $res =  $this->up($where, $data);
        } else {
            $time = date('Y-m-d H:i:s', time());
            $data['created_at'] = $time;
            $data['updated_at'] = $time;
            $res = $this->add($data);
        }
        $ajaxData['status'] = 1;
        $ajaxData['msg'] = '保存成功';
        if (!$res) {
            $ajaxData['status'] = 0;
            $ajaxData['msg'] = '保存失败';
        }
        return $ajaxData;
    }

    /**
     *上传多图  仅限 base64
     *
     * @param $imgs
     * @param bool|false $dir 路径
     * @param bool|true $isdel
     * @return string
     */
    public function upImgs($imgs, $dir = false)
    {
        $dir = $dir ? $dir : date('Ymd');
        $res = '';
        $imgpath = "uploads/reply/" . $dir;
        if (!is_array($imgs)) {
            $res = $this->upload($imgs,$imgpath);
            return $res;
        }
        foreach ($imgs as $k => $v) {
            if (file_exists($v)) {
                $res[$k] = $v;
            } else {
                $v = explode(',', $v);
                $save = $this->upload($v,$imgpath);
                if ($save) {
                    $res[$k] = $save;
                }
            }
        }
        return $res;
    }

    public function upload($file,$path)
    {
        $v = explode(',', $file);
        preg_match('/image\/(.*);base64/u', $v[0], $imgtype);
        $imgtype = $imgtype[1] == "jpeg" ? 'jpg' : $imgtype[1];
        if (!is_dir($path)) {
            mkdir($path);
        }
        list($usec, $sec) = explode(" ", microtime());
        $imgname = $sec . str_replace('0.', '_', $usec);
        $imgname = $imgname . '.' . $imgtype;
        $base64 = base64_decode(end($v));
        $res = file_put_contents($path . "/". $imgname, $base64);
        if ($res) {
            return $path. "/".$imgname;
        }
        return false;
    }


}